<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from '@/mixins/resize'

require('echarts/theme/macarons') // echarts theme

// const animationDuration = 6000

export default {
  name: 'Chart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    option: {
      type: Object,
      required: true
    },
    height: {
      type: String,
      default: '300px'
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.handleLoading()
    })
  },
  beforeDestroy() {
    this.destroyCart()
  },
  watch: {
    option() {
      this.destroyCart()
      this.initChart()
      // this.chart.setOption(this.option)
    },
    loading() {
      this.handleLoading()
    }
  },
  methods: {
    handleLoading() {
      if (this.loading) {
        this.chart.showLoading()
      } else {
        this.chart.hideLoading()
      }
    },
    destroyCart() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(this.option)

    }
  }
}
</script>
